#' Get Wight Matrix
#' @param xt: charactor vector. It looks like "ARG_cpi" "ARG_gdp"
#' "ARG_TFP" "AUS_cpi" "AUS_gdp" "AUS_TFP"...
#' @param TradeWeight: dataframe. The colsum = 1
#' #algorithm
#' #-------------------------
#' # W =  0 0 0 1 0 0 0 0 0
#' #      0 0 0 0 1 0 0 0 0  up matrix
#' #      0 0 0 0 0 1 0 0 0
#' #-------------------------
#' #      w1 0 0 0 0 0 w3 0 0
#' #      0 w1 0 0 0 0 0 w3 0 down matrix
#' #      0 0 w1 0 0 0 0 0 w3
#' #-------------------------
GetW <- function(xt, tradeweight){
  library(matlab)
  library(magrittr)
  cnt <- unique(str_sub(xt,1,3))
  W <- list()
  for (i in 1:length(cnt)) {
    n <- length(which(str_detect(xt,cnt[i]))) # get the number of endogenous variable
    wup <- zeros(n,length(xt))
    wup[,(1 + n*(i-1)):(n*i)] <- eye(n)

    wdown <- NULL
    for (j in 1:length(cnt)) {
      wdown <- (eye(n) * tradeweight[j,i]) %>% cbind(wdown,.)
    }
    W[[cnt[i]]] <- rbind(wup,wdown)
  }
  return(W)
}
